
/**
 * Date:2022年7月28日下午5:26:09
 * Copyright (c) 2022, www.bwbroad.com All Rights Reserved.
 *
*/

package com.action;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Demo;
import com.pojo.Page;

import freemarker.template.utility.StringUtil;

/**
 * Description: TODO <br/>
 * Date:     2022年7月28日 下午5:26:09 <br/>
 * @author   wangbing	 
 */
@Controller
@RequestMapping("/")
public class TestController {

	@GetMapping("j/index")
	 public String jIndex(Model model,HttpServletRequest request) {
		model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
		return "jsp/index";
	 }
	
	
	@GetMapping("j/list")
	public String jList(Model model, Page<Demo> page) {
		if (null == page) {
			page = new Page<>(1, 2);
		}
		List<Demo> list = getList("jsp");
		page.setCount(list.size());
		page.setList(list);
		model.addAttribute("page", page);
		return "jsp/index";
	}
	
	

	
	@GetMapping("j/dialog1")
	 public String dialog1(Model model,HttpServletRequest request) {
		String param1 = request.getParameter("param1");
		String callback = request.getParameter("callback");
		 Page<Demo> page = new Page<Demo>(1, 2);
		List<Demo> list = getList("jsp",5);
		page.setCount(list.size());
		page.setList(list);
		model.addAttribute("page", page);
		model.addAttribute("param1", param1);
		model.addAttribute("callback", callback);
		return "jsp/dialog1";
	 }
	@GetMapping("f/getById")
	public String getById(Model model,HttpServletRequest request) {
		String param1 = request.getParameter("getById");
		model.addAttribute("id", param1);

		return "freemarker/view";
	}
	
	
	@GetMapping("t/index")
	 public String tIndex(Model model) {
	  System.out.println("t index");
	  model.addAttribute("msg", "hello thymeleaf:" + LocalDateTime.now());
	  return "thymeleaf/index";
	 }
	
	@RequestMapping("t/list")
	public String tList(Model model, Page<Demo> page) {
		if (null == page) {
			page = new Page<>(1, 2);
		}
		List<Demo> list = getList("thy");
		page.setCount(list.size());
		page.setList(list);
		model.addAttribute("page", page);
		return "thymeleaf/list";
	}
	
	@GetMapping("f/index")
	 public String fIndex(Model model) {
	  System.out.println("f index");
	  model.addAttribute("msg", "hello freemarker:" + LocalDateTime.now());
	  return "freemarker/index";
	 }
	
	
	@GetMapping("f/list")
	public String fList(Model model) {
		model.addAttribute("list", getList("frem"));
		return "freemarker/index";
	}
	
	@PostMapping("save/demo")
	@ResponseBody
	public Map<String, Object> saveDemo(@RequestBody Demo d) {
		System.out.println(d);
		return Map.of("success", true);
	}
	
	@PostMapping("demo/deleteByIds")
	@ResponseBody
	public Map<String, Object> deleteByIds(@RequestBody List<String> ids) {
		System.out.println(ids);
		return Map.of("success", true);
	}

	@RequestMapping("j/getProList")
	public String getProList(Model model, String id) {
		model.addAttribute("proList",getProvince("北京"));
		return "jsp/province";
	}
	
	@RequestMapping("t/getCityList")
	public String getCityList(Model model, String pid) {
		List<Demo> demoList = new ArrayList<Demo>();
		if (pid!=null && !pid.isEmpty()) {
			demoList = getCity(pid+"省id");
		}
		model.addAttribute("cityList",demoList);
		return "thymeleaf/index";
	}
	
	@GetMapping("f/getCountyList")
	public String getCountyList(Model model, String pid) {
		if (pid!=null && !pid.isEmpty()) {
			model.addAttribute("countyList",getCounty("市id"+pid));
		}
		return "freemarker/index";
	}
	
	
	private List<Demo> getList(String name) {
		  return IntStream.range(1,20).mapToObj(i -> new Demo(i+"", name + "_name_" + i)).collect(Collectors.toList());
		}
	
	private List<Demo> getList(String name,int pageSize) {
	  return IntStream.range(1, pageSize).mapToObj(i -> new Demo(i+"", name + "_name_" + i)).collect(Collectors.toList());
	}
	private List<Demo> getProvince(String id) {
		  return IntStream.range(1,10).mapToObj(i -> new Demo(i+"", id + "_省_" + i)).collect(Collectors.toList());
	}
	private List<Demo> getCity(String id) {
		  return IntStream.range(1,10).mapToObj(i -> new Demo(i+"", id + "_市_" + i)).collect(Collectors.toList());
	}
	private List<Demo> getCounty(String id) {
		  return IntStream.range(1,10).mapToObj(i -> new Demo(i+"", id + "_县_" + i)).collect(Collectors.toList());
	}
	
}

